﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Text;
using ITPCTaxFormModel;

partial class Login : System.Web.UI.Page
{
    //public string ResolveURL()
    //{
    //    return "http://" + Request.UserHostName + ":" + Request.Url.Port + Request.Url.Segments[0].ToString().Replace("//", "/") + Request.Url.Segments[1].ToString().Replace("//", "/");
    //}

     public string ResolveUrl(string url) 
     { 
        if (url==null) throw new ArgumentNullException("url", "url can not be null"); 
        if (url.Length == 0) throw new ArgumentException("The url can not be an empty string", "url"); 
  
        // there is no ~ in the first character position, just return the url 
        if (url[0] != '~') return url; 
  
        string applicationPath = HttpContext.Current.Request.ApplicationPath; 
  
        // there is just the ~ in the URL, return the applicatonPath 
        if (url.Length == 1) return applicationPath; 
  
        // assume url looks like ~somePage 
        int indexOfUrl=1; 
  
        // determine the middle character 
        string midPath = (applicationPath.Length >1 )? "/" : string.Empty; 
  
        // if url looks like ~/ or ~\ change the indexOfUrl to 2 
        if (url[1] == '/' || url[1] == '\\') indexOfUrl=2; 
  
        return applicationPath + midPath + url.Substring(indexOfUrl); 
     }// ResolveUrl

    public void Excute_Javascript(string strFunction, object cls, bool addScriptTags = true)
    {
        Page.RegisterClientScriptBlock("ClientScript", strFunction);
    }
    public string EncodePass(string strPAss)
    {
        string strCode = null;
        MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
        byte[] hashedDataBytes = null;
        UTF8Encoding encoder = new UTF8Encoding();
        hashedDataBytes = md5Hasher.ComputeHash(encoder.GetBytes(strPAss));
        strCode = Convert.ToBase64String(hashedDataBytes);
        return strCode;
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        using (var ett_context = new ITPCTaxFormEntities1())
        {
            var UsrName = txtUserName.Text;
            var Psswrd = EncodePass(txtPassword.Text);
            var authen = ett_context.Users.Where(u => u.UserName.Equals(UsrName)).Select(u => u).ToList();
            //Pass authentication
            if (authen.Count.Equals(1))
            {
                if (authen[0].Password.Equals(Psswrd))
                {
                    if (authen[0].Roles.Equals(3))
                    {
                        //set User session
                        Session["gUserID"] = authen[0].UserID;
                        Session["gUserName"] = authen[0].UserName;
                        Session["gRoles"] = authen[0].Roles;
                        //redirect to homepage
                        Response.Redirect(ResolveUrl("~/Default.aspx"));
                    }
                    else {
                        string strJs = "<SCRIPT LANGUAGE='JavaScript'> ";
                        strJs += "alert('Người dùng không có quyền quản trị! Vui lòng liên hệ người quản trị hệ thống')";
                        strJs += "</SCRIPT>";
                        Excute_Javascript(strJs, this.Page);
                    }
                }
                else
                {
                    string strJs = "<SCRIPT LANGUAGE='JavaScript'> ";
                    strJs += "alert('Mật khẩu không đúng!')";
                    strJs += "</SCRIPT>";
                    Excute_Javascript(strJs, this.Page);
                }
            }
            else
            {
                string strJs = "<SCRIPT LANGUAGE='JavaScript'> ";
                strJs += "alert('Tên đăng nhập không đúng!');";
                strJs += "</SCRIPT>";
                Excute_Javascript(strJs, this.Page);
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["gUserID"] != null)
            Response.Redirect(ResolveUrl("~Default.aspx"));
    }
}
